package com.es.hotel.main.client;

import com.alibaba.fastjson.JSON;
import com.es.hotel.pojo.Hotel;
import com.es.hotel.pojo.HotelDoc;
import com.es.hotel.service.IHotelService;
import com.es.hotel.service.impl.HotelService;
import org.apache.lucene.index.IndexReader;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class EsDocClient {


    //通过构造函数传入service
    @Autowired
    private IHotelService hotelService;


    //新增文档
     public void addDoc() throws IOException {
        Hotel hotel = hotelService.getById(36934L);
        //地理位置数据库是经纬度，es是一个类型
        HotelDoc hotelDoc = new HotelDoc(hotel);
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
        request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
        //发送请求
        EsIndexClient.client().index(request, RequestOptions.DEFAULT);
    }

    //查看文档
    public HotelDoc getDoc() throws IOException {
        Hotel hotel = hotelService.getById(36934L);
        GetRequest request = new GetRequest("hotel", hotel.getId().toString());
        //发送请求解析响应
        GetResponse response = EsIndexClient.client().get(request, RequestOptions.DEFAULT);
        return JSON.parseObject(response.getSourceAsString(), HotelDoc.class);
    }

    //修改文档
    public void updateDoc() throws IOException {
         Hotel hotel = hotelService.getById(36934L);
        UpdateRequest request = new UpdateRequest("hotel", hotel.getId().toString());
        request.doc(
                "age",18,
                "name","jia"
        );
        EsIndexClient.client().update(request, RequestOptions.DEFAULT);
    }

    //删除文档
    public void deleteDoc() throws IOException {
        //Hotel hotel = hotelService.getById(36934L);
        //批量删除
        List<Hotel> hotels = hotelService.list();
        for(Hotel hotel:hotels){
            DeleteRequest request = new DeleteRequest("hotel", hotel.getId().toString());
            EsIndexClient.client().delete(request, RequestOptions.DEFAULT);
        }

    }

    //批量导入文档
    public void bulkDoc() throws IOException{
        BulkRequest request = new BulkRequest();
        List<Hotel> hotels = hotelService.list();
        for(Hotel hotel:hotels){
            HotelDoc hotelDoc = new HotelDoc(hotel);
            String docId = hotel.getId().toString();
            //幂等性处理
            GetRequest getRequest = new GetRequest("hotel", docId);
            boolean exists = EsIndexClient.client().exists(getRequest, RequestOptions.DEFAULT);
            if(!exists){
                request.add(new IndexRequest("hotel")
                        .id(docId)
                        .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
            }
        }
        if(request.numberOfActions() > 0){
            EsIndexClient.client().bulk(request,RequestOptions.DEFAULT);
        }else{
            System.out.println("所有文档已经存在，无需导入");
        }
    }
}
